﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace Lab6_Product
{
    public partial class frmAddNewProduct : Form
    {
        public delegate void RloadData();
        int newID;
        string imagePath="";
        string name = "";
        string des = "";
        int priceIn = 0;
        int priceSell = 0;
        int quantity = 0;

        public frmAddNewProduct()
        {
            InitializeComponent();
        }

        

        private void btnImage_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string file = openFileDialog1.FileName;
                    string fileName = file.Substring(file.LastIndexOf(@"\"), file.Length - file.LastIndexOf(@"\"));
                    //Image img = Image.FromFile(@file);

                    string exeFolder = Application.StartupPath;
                    string path1 = exeFolder.Substring(0, exeFolder.LastIndexOf(@"\"));
                    string path2 = path1.Substring(0, path1.LastIndexOf(@"\"));
                    string imgPath = Path.Combine(path2, @"image" + fileName);
                    byte[] b = null;
                    using (FileStream f = new FileStream(@file, FileMode.Open))
                    {
                        b = new byte[f.Length];

                        f.Read(b, 0, b.Length);
                    }

                    // Write to file ...
                    using (FileStream fs = new FileStream(@imgPath, FileMode.Create))
                    {
                        fs.Write(b, 0, b.Length);

                    }
                    Image img = Image.FromFile(file);
                    pictureBox1.Image = img;
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                    if (!"".Equals(imgPath))
                    {
                        imagePath = imgPath;
                    }
                    else imagePath = "";                                 
             }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            if (check())
            {
                try
                {
                    newID = Convert.ToInt32(txtID.Text);
                    name = txtName.Text;
                    des = txtDes.Text;
                    priceIn = Convert.ToInt32(txtPriceIn.Text);
                    priceSell = Convert.ToInt32(txtPriceSell.Text);
                    quantity = Convert.ToInt32(txtQuantity.Text);
                    DataProccess dp = new DataProccess();
                    if (dp.AddNewProduct(newID, name, des, priceIn, priceSell, quantity, imagePath))
                    {
                        MessageBox.Show("Add new product success!");

                        //Clear all text boxes (controls)
                        ClearTextBoxes(this);

                        //Generate new ID 
                        DataProccess d = new DataProccess();
                        DataTable dt = d.GetAllProduct();
                        newID = dt.Rows.Count + 1;
                        txtID.Text = newID.ToString();

                        //Refresh datagridview 
                        //frmViewAllProducts fr = new frmViewAllProducts();
                        //RloadData r = new RloadData(fr.loadForm);
                        //r();
                        frmViewAllProducts.GetObj().loadForm();
                        frmViewAllProducts.GetObj().frmViewAllProducts_Load(sender, e);
                                              
                    }
            }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }
            }
        }

        public void ClearTextBoxes(Control control)
        {
            foreach (Control c in control.Controls)
            {

                if (c is TextBox)
                {

                    ((TextBox)c).Clear();

                }

                if (c.HasChildren)
                {

                    ClearTextBoxes(c);

                }

            }
        }

        private void frmAddNewProduct_Load(object sender, EventArgs e)
        {
           DataProccess dp = new DataProccess();
           DataTable dt = dp.GetAllProduct();
           newID = dt.Rows.Count +1;
           txtID.Text = newID.ToString();
        }

        public bool check()
        {
            if (txtID.Text.Length == 0)
            {
                MessageBox.Show("Why u clear ID, ur ID is " + newID + ", input it again -.- ");
                txtID.Focus();
                return false;
                
            }
            else if (txtName.Text.Length == 0)
            {
                MessageBox.Show("Are u kidding me! A product without name, is it an invisible product?");
                txtName.Focus();
                return false;
            }
            else if (txtPriceIn.Text.Length == 0)
            {
               DialogResult pInDialog= MessageBox.Show("Is your product really free? Congrat, u will get a lot cash when u sell it!","Really?",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
               if (pInDialog == DialogResult.No)
               {
                   txtPriceIn.Focus();
                   return false;
               }
               else
               {
                   txtPriceIn.Text = "0";
                   return true; }
            }
            else if (txtPriceSell.Text.Length == 0)
            {
                MessageBox.Show("Want to sell your product FREE? I guess u just forget input Selling Price", "Selling Price", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPriceSell.Focus();
                return false;
            }
            else if (txtQuantity.Text.Length == 0)
            {
                MessageBox.Show("Please input quantity");
                txtQuantity.Focus();
                return false;
            }
            else return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
